import { WorkflowPredefineDb } from '/@/enums/flowManage';

export const mainColumnOptions = [
  {
    value: 'comb_field',
    label: '表单字段',
  },
  {
    value: 'comb_priv_role',
    label: '上一节点用户角色',
  },
  {
    value: 'comb_priv_dept',
    label: '上一节点用户部门',
  },
];
export const firstBracketOptions = [
  {
    value: '',
    label: '',
  },
  {
    value: '(',
    label: '(',
  },
  {
    value: ')',
    label: ')',
  },
];

export const compareOptions = [
  {
    value: '=',
    label: '=',
  },
  {
    value: '>=',
    label: '>=',
  },
  {
    value: '<=',
    label: '<=',
  },
  {
    value: '>',
    label: '>',
  },
  {
    value: '<',
    label: '<',
  },
  {
    value: '<>',
    label: '<>',
  },
];
export const compareOptions2 = [
  {
    value: '=',
    label: '=',
  },
  {
    value: '<>',
    label: '<>',
  },
  {
    value: '=>',
    label: '=>',
  },
];
export const compareOptions3 = [
  {
    value: '=',
    label: '=',
  },
  {
    value: '<>',
    label: '<>',
  },
];

export const logicalOptions = [
  {
    value: 'and',
    label: '并且',
  },
  {
    value: 'or',
    label: '或者',
  },
];

export const columns = [
  {
    title: '条件字段',
    dataIndex: 'field',
    key: 'field',
    align: 'center',
    slots: { customRender: 'field' },
    width: 200,
  },
  {
    title: '跳转条件',
    dataIndex: 'tztj',
    key: 'tztj',
    align: 'center',
    slots: { customRender: 'tztj' },
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    key: 'action',
    slots: { customRender: 'action' },
    width: 100,
  },
];

export const setMsgProp = (list, { configFrom, configTo }) => {
  // var tb = document.getElementById("tab");
  const links = '<links>'; //<?xml version="1.0" encoding="UTF-8"?>
  let str = '';
  let val = '';
  list.forEach((item) => {
    console.log('item', item);
    const random = Math.random();
    const link = '<link id="' + random + '">';
    const from = '<from>' + item.from + '</from>';
    const to = '<to>' + item.to + '</to>';
    const mainColumn = '<name>' + item.mainColumn + '</name>';
    const firstBracket = '<firstBracket>' + item.firstBracket + '</firstBracket>';
    const fieldName = '<fieldName>' + item.fieldName + '</fieldName>';
    let compare = item.compare;
    // > < &gt; &lt;
    compare = compare.replaceAll('<', '&lt;');
    compare = compare.replaceAll('>', '&gt;');
    const operator = '<operator>' + compare + '</operator>';
    const logical = '<logical>' + item.logical + '</logical>';
    const twoBracket = '<twoBracket>' + item.twoBracket + '</twoBracket>';

    if (item.mainColumn == WorkflowPredefineDb.COMB_COND_TYPE_FIELD) {
      val = '<value>' + item.columnName + '</value>';
    }
    if (item.mainColumn == WorkflowPredefineDb.COMB_COND_TYPE_PRIV_ROLE) {
      val = '<value>' + item.role + '</value>';
    }
    if (item.mainColumn == WorkflowPredefineDb.COMB_COND_TYPE_PRIV_DEPT) {
      val = '<value>' + item.userDept + '</value>';
    }
    const fieldType = '<fieldType>' + item.fieldType + '</fieldType>';
    const endLink = '</link>';
    str +=
      link +
      from +
      to +
      firstBracket +
      fieldName +
      mainColumn +
      operator +
      val +
      twoBracket +
      logical +
      fieldType +
      endLink;
  });
  if (list.length == 0) {
    const from = '<from>' + configFrom + '</from>';
    const to = '<to>' + configTo + '</to>';
    str += '<link>' + from + to + '</link>';
  }
  const endLinks = '</links>';

  const hiddenCondition = links + str + endLinks;
  return hiddenCondition;
  // let len = list.value.length - 1;
  // let maxIndex = $('#maxIndex').val();
  // let currentMaxIndex = parseInt($('#currentMaxIndex').val()) + 1;
  // if (currentMaxIndex > maxIndex) {
  //   len = currentMaxIndex;
  // } else {
  //   if (maxIndex > len) {
  //     len = maxIndex;
  //   }
  // }

  // for (let i = 0; i < len; i++) {
  //   let obj = document.getElementById('fieldName' + i);
  //   if (obj != null) {
  //     let index = obj.selectedIndex;

  //     let random = Math.random();
  //     let link = '<link id="' + random + '">';
  //     let from = '<from><%=fromValue%></from>';
  //     let to = '<to><%=toValue%></to>';
  //     let mainColumn = '<name>' + $('#mainColumn' + i).val() + '</name>';
  //     let firstBracket = '<firstBracket>' + $('#firstBracket' + i).val() + '</firstBracket>';
  //     let fieldName = '<fieldName>' + $('#fieldName' + i).val() + '</fieldName>';
  //     let compare = $('#compare' + i).val();
  //     // > < &gt; &lt;
  //     compare = compare.replaceAll('<', '&lt;');
  //     compare = compare.replaceAll('>', '&gt;');
  //     let operator = '<operator>' + compare + '</operator>';
  //     let logical = '<logical>' + $('#logical' + i).val() + '</logical>';
  //     let twoBracket = '<twoBracket>' + $('#twoBracket' + i).val() + '</twoBracket>';

  //     if ($('#mainColumn' + i).val() == '<%=WorkflowPredefineDb.COMB_COND_TYPE_FIELD%>') {
  //       val = '<value>' + $('#columnName' + i).val() + '</value>';
  //     }
  //     if ($('#mainColumn' + i).val() == '<%=WorkflowPredefineDb.COMB_COND_TYPE_PRIV_ROLE%>') {
  //       val = '<value>' + $('#role' + i).val() + '</value>';
  //     }
  //     if ($('#mainColumn' + i).val() == '<%=WorkflowPredefineDb.COMB_COND_TYPE_PRIV_DEPT%>') {
  //       val = '<value>' + $('#userDept' + i).val() + '</value>';
  //     }
  //     let fieldType = '<fieldType>' + obj.options[index].getAttribute('id') + '</fieldType>';
  //     let endLink = '</link>';
  //     str +=
  //       link +
  //       from +
  //       to +
  //       firstBracket +
  //       fieldName +
  //       mainColumn +
  //       operator +
  //       val +
  //       twoBracket +
  //       logical +
  //       fieldType +
  //       endLink;
  //   }
  // }
  // let endLinks = '</links>';
  // dlg.setCondition(links+str+endLinks);

  // let sb = ''; //new StringBuilder();
  // sb = '<action internalName="' + useFlow.flowInfo.curInternalName + '">\r\n';
  // sb += '<actionNames>' + strActions + '</actionNames>\r\n';
  // sb += '<deptFields>' + strDepts + '</deptFields>\r\n';
  // sb += '<userFields>' + strUsers + '</userFields>\r\n';
  // sb += '<users>' + formData.value['users'] + '</users>\r\n';
  // sb += '<roles>' + formData.value['roles'] + '</roles>\r\n';
  // sb += '<title>' + formData.value['title'] + '</title>\r\n';
  // sb += '<content>' + formData.value['content'] + '</content>\r\n';
  // sb += '<isMsg>' + formData.value['isMsg'] + '</isMsg>\r\n';
  // sb += '<isSms>' + formData.value['isSms'] + '</isSms>\r\n';
  // sb += '<isMail>' + formData.value['isMail'] + '</isMail>\r\n';
  // sb += '<isFlowShow>' + formData.value['isFlowShow'] + '</isFlowShow>\r\n';
  // sb += '</action>\r\n';
  // console.log('sb', sb);
  // // <textarea id="xmlContent" style="display:none"><%=sb.toString() %></textarea>
  // // <script>
  // let xml: any = linkProps.value; //window.opener.getMsgProp();
  // if (xml == '') {
  //   xml = '<links></links>';
  // }
  // try {
  //   xml = $.parseXML(xml);
  // } catch (e) {
  //   alert(e);
  // }

  // let $xml = $(xml);
  // $xml
  //   .find('links')
  //   .children()
  //   .each(function (i) {
  //     if (
  //       $(this).find('from').text() == from.value &&
  //       $(this).find('to').text() == to.value
  //     ) {
  //       $(this).remove();
  //     }
  //   });

  // let $elem = $($.parseXML(sb)); //o("xmlContent").value
  // let newNode = null;
  // if (typeof document.importNode == 'function') {
  //   newNode = document.importNode($elem.find('action').get(0), true);
  // } else {
  //   newNode = $elem.find('action').get(0);
  // }
  // $xml.find('links').get(0).appendChild(newNode);
  // linkProps.value = $xml.xml();
  // useFlow.setFlowEdit({ ...useFlow.flowEdit, linkProps: unref(linkProps) });
  // console.log('保存成功', linkProps.value);
  // window.opener.setMsgProp($xml.xml());
};
